﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum
{
    public partial class Zhuche : Form
    {
        public Zhuche()
        {
            InitializeComponent();
        }

        private void Zhuche_Load(object sender, EventArgs e)
        {
            Verify_Click(sender, e);
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Close();//单关闭单个界面
            
        }

        public static string sql;
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var AccountPass = @"^\d*$";//合法账号的正则表达式
            var PrsswordPass = @"^\d*[a-z]*$";




            if (userPwd.Text == "" || textAccount.Text == "")//判断用户输入是否为空
            {
                MessageBox.Show("账号和密码不能为空!");
            }
            else if (Regex.IsMatch(textAccount.Text, AccountPass) == false)
            {
                MessageBox.Show("请输入合法账号");
            }
            else if (Regex.IsMatch(userPwd.Text, PrsswordPass)==false)
            {
                MessageBox.Show("请输入合法密码");
            }
            else//账号与密码均属于合法就进行下一步
            {
                //进行查询
                string sql2 = string.Format("select UserAccount from Users where UserAccount='{0}'", textAccount.Text);
                SqlDataReader reader = DBHelps.Select(sql2);

                if (reader.Read())//判断账号是否存在
                {
                    MessageBox.Show("账号已存在请重新输入");
                    reader.Close();
                    DBHelps.reader = null;
                }
                else//如果不存在则创建
                {
                    if (checkBox1.Checked)
                    {
                        reader.Close();
                        DBHelps.reader = null;
                        //判断无异常就对数据库进行赋值注册
                        sql = string.Format("insert into Users(UserName,UserAccount,UserPwd,Sex) values('{0}', '{1}', '{2}','{3}')", textUserName.Text, textAccount.Text, userPwd.Text, comboBox1.Text);
                        if (DBHelps.Command(sql))//判断是否有影响行数
                        {
                            MessageBox.Show("注册成功！！！");
                            Close();//退出注册界面
                        }
                        else
                        { MessageBox.Show("注册失败");}
                    }
                    else { MessageBox.Show("请阅读并同意条款！"); }
                }
            }
        }

        private void textUserName_TextChanged(object sender, EventArgs e)
        {
            if (textUserName.Text=="" )
            {
                label1.Visible = true;
            }
            else
            {
                label1.Visible = false;
            }
        }

        private void textAccount_TextChanged(object sender, EventArgs e)
        {
            if (textAccount.Text == "")
            {
                label2.Visible = true;
            }
            else
            {
                label2.Visible = false;
            }
        }

        private void userPwd_TextChanged(object sender, EventArgs e)
        {
            if (userPwd.Text == "")
            {
                label3.Visible = true;
            }
            else
            {
                label3.Visible = false;
            }
        }

        private void textUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            //在输入昵称框中键盘回车可以进行注册
            if (e.KeyChar == (char)13)//ASCII 13为回车的码
            {
                button1_Click(sender, e);//使用注册按钮方法
            }
        }

        private void textAccount_KeyPress(object sender, KeyPressEventArgs e)
        {
            //在输入用户账号框中键盘回车可以进行注册
            if (e.KeyChar == (char)13)//ASCII 13为回车的码
            {
                button1_Click(sender, e);//使用注册按钮方法
            }
        }

        private void userPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            //在输入用户密码框中键盘回车可以进行注册
            if (e.KeyChar == (char)13)//ASCII 13为回车的码
            {
                button1_Click(sender, e);//使用注册按钮方法
            }
        }

        private void checkBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //勾选框回车也可以跳转到注册
            if (e.KeyChar == (char)13)//ASCII 13为回车的码
            {
                button1_Click(sender, e);//使用注册按钮方法
            }
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //选择框回车也可以跳转到注册
            if (e.KeyChar == (char)13)//ASCII 13为回车的码
            {
                button1_Click(sender, e);//使用注册按钮方法
            }
        }
        //无边框拖动窗体-----------------------------------------------------------------------------------
        [DllImport("user32.dll")]//*********************拖动无窗体的控件  需要引用
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int W = 0x0112;
        public const int S = 0xF010;
        public const int H = 0x0002;
        private void Zhuche_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, W, S + H, 0);//*********************调用移动无窗体控件函数
        }

        private void Verify_Click(object sender, EventArgs e)
        {
            Bitmap map = new Bitmap(100, 30);//生成一张空白的图片宽：150px，高：50px
            Graphics gs = Graphics.FromImage(map);//用GDI读取这个图片进行操作

            Random myrandom = new Random();//生成随机数

            string verify = "";//这是最终组合成的验证码
            for (int i = 0; i < 5; i++)
            {
                int x1 = myrandom.Next(0, 10);
                verify += x1;
            }

            string[] myfont = { "宋体", "仿宋", "微软雅黑", "隶书", "方正姚体" };//存字体样式的数组
            Color[] mycolor = { Color.Red,Color.Green,Color.Blue,Color.Black,Color.Orange };

            for (int i = 0; i < 5; i++)
            {

                int x2 = myrandom.Next(0, 5);

                Point p = new Point(i * 20, 0);
                gs.DrawString(verify[i].ToString(),new Font(myfont[x2],16,FontStyle.Bold),new SolidBrush(mycolor[i]),p);
                //第1个参数是画的东西是什么（我们验证码的字符串）;第2个参数是字体;
            }

            for (int i = 0; i < 18; i++)//画18条干扰线
            {
                Point p1 = new Point(myrandom.Next(0,map.Width),myrandom.Next(0,map.Height));//在随机的起点和随机的终点之间画一条线
                Point p2 = new Point(myrandom.Next(0, map.Width), myrandom.Next(0, map.Height));//在随机的起点和随机的终点之间画一条线
                gs.DrawLine(new Pen(Color.Red), p1, p2);
            }

            for (int i = 0; i < 300; i++)//画500个干燥点
            {
                Point p3 = new Point(myrandom.Next(0, map.Width), myrandom.Next(0, map.Height));
                map.SetPixel(p3.X,p3.Y,Color.Black);//三个参数：x轴，y轴，颜色
            }
            

            Verify.Image = map;
            //MessageBox.Show(verify);

        }
        //无边框拖动窗体-----------------------------------------------
    }
}
